package com.example.leero.graduation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.leero.graduation.customView.LoadingBar;
import com.example.leero.graduation.customView.LoadingDialog;

public abstract class BaseActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutId());

    initVariable();
    initView(savedInstanceState);
    loadData();
  }

  protected abstract int getLayoutId();

  protected void initVariable() {}

  protected abstract void initView(Bundle state);

  protected void loadData(){}

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  protected void post(Runnable runnable) {
    post(runnable, 500);
  }

  protected void post(Runnable runnable, long delayMillis) {
    findViewById(android.R.id.content).postDelayed(runnable, delayMillis);
  }

  protected void dismissDialog() {
    post(new Runnable() {
      @Override
      public void run() {
        LoadingDialog.dismissDialog();
      }
    });
  }

  /**
   * 显示加载框
   * @param parent
   */
  protected void showLoading(View parent) {
    LoadingBar.show(parent);
  }

  /**
   * 显示错误框，点击重试
   */
  protected void showError(View parent, View.OnClickListener listener) {
    LoadingBar.show(parent, View.inflate(this, R.layout.layout_error, null), listener);
  }

  /**
   * 隐藏加载框
   * @param parent
   */
  protected void dismissLoading(View parent) {
    LoadingBar.cancel(parent);
  }

}
